function rttplot(fname1, fname2, tick1, trange, yrange)
% RTTPLOT  Plot the RTT, RTO and RTO counter's value.
%    RTTPLOT(FILENAME1, FILENAME2, TICK, XRANGE, YRANGE) plots the specified 
%    data file about the start point and end point of RTT measurement and 
%    estimated timeout value as well as RTO timer counter's value.
%    FILENAME1 RTT output data file
%    FILENAME2 RTO output data file
%    TICK      the tick value of the first vertical grid line
%    XRANGE    the range of the x-axis
%    YRANGE    the range of the y-axis
%
% Example:
%       rttplot('rttdump.out','rexdump.out', 0.3, [192 196]);
%       rttplot('rttdump.out','rexdump.out', 0.3, [192 196], [0 6]);

if (nargin < 4) | (nargin > 5)
   error('Wrong number of input arguments. Try help rttplot.');
end

if nargin > 3
   stime = trange(1);
   etime = trange(2);
else
   stime = -1;
end

if nargin > 4
   RTOmin = yrange(1);
   RTOmax = yrange(2);
else
   RTOmin = -1;
end


ldcmd = sprintf('load %s;', fname1);
eval(ldcmd);
fnamehd = strtok(fname1, '.');
asgncmd = sprintf('RTT = %s;', fnamehd);
eval(asgncmd);
clrcmd = sprintf('%s = [];', fnamehd);
eval(clrcmd);

ldcmd = sprintf('load %s;', fname2);
eval(ldcmd);
fnamehd = strtok(fname2, '.');
asgncmd = sprintf('D = %s;', fnamehd);
eval(asgncmd);
clrcmd = sprintf('%s = [];', fnamehd);
eval(clrcmd);

[row0 col0] = size(RTT);
[row col] = size(D);
if col0 < 3
   error('Not enough columns in rtt file.');
end
if col < 5
   error('Not enough columns in rto file.');
end

if stime >= 0
   T = [];
   numT = 0;
   for i=1:row
      if (stime <= D(i,1)) & (D(i,1) <= etime)
         numT = numT + 1;
	 T(numT,:) = D(i,:);
      end
   end
   D = T;
   T = [];
   numT = 1;
   i = 2;
   while i < (row0 + 1)
      if (stime <= RTT(i,2)) & (RTT(i,2) <= etime)
         if RTT(i,1) == 1
	   if RTT(i-1,1) == 0
             T(numT,:) = RTT(i-1,:);
             T(numT+1,:) = RTT(i,:);
             T(numT,3) = RTT(i,3);
             numT = numT + 2;
           end
           i = i + 2;
	 else
	   i = i + 1;
	 end
      else
        i = i + 1;
      end
   end
else
   T = [];
   numT = 1;
   i = 2;
   while i < (row0 + 1)
     if RTT(i,1) == 1
       if RTT(i-1,1) == 0
         T(numT,:) = RTT(i-1,:);
         T(numT+1,:) = RTT(i,:);
         T(numT,3) = RTT(i,3);
         numT = numT + 2;
       end
       i = i + 2;
     else
       i = i + 1;
     end
   end
end
RTT = T;
T = [];

z = 0;
label = '';

if nargin > 2
  if nargin > 3
    tick2 = etime;
  else
    tick2 = tick1 + 20;
  end

  cmd = sprintf('tick = [');
  for i=tick1: 0.5 : tick2 
    z = z+1;
    str = sprintf(' %f',i);
    cmd = strcat(cmd,str);
    if mod(z,2) == 1
      lab = sprintf('%8.2f | | ',i);
      label = strcat(label,lab);
    end
  end
  cmd = strcat(cmd,'];');
  eval(cmd);
end

%set(gcf,'Units','pixels');
%set(gcf,'Position',[20 40 800 900]);

oplot = 0;
if (~isempty(D)) & (max(D(:,4)) >= 0)
  plot(D(:,1),D(:,4),'-+r','MarkerSize',4);
  oplot = 1;
end
hold on;
splot = 0;
if (~isempty(D)) & (max(D(:,3)) >= 0)
  plot(D(:,1),D(:,3),'-.sm','MarkerSize',2);
  splot = 1;
end

tplot = 0;
[row0 col0] = size(RTT);
if (~isempty(RTT)) & (max(RTT(:,2)) >= 0)
  for i=1:2:row0-1
    t = [];
    t(1,1) = RTT(i,2);
    t(1,2) = RTT(i,3);
    t(2,1) = RTT(i+1,2);
    t(2,2) = RTT(i+1,3);
    if t(2,2) ~= -0.5
      plot(t(:,1),t(:,2),'-ob','MarkerSize',4);
    end
  end
  tplot = 1;
end

xlabel('Time (seconds)');
ylabel('RTT,SRTT/RTO (seconds)');
if nargin > 2
  set(gca,'XTick',tick);
  set(gca,'XTickLabel',label);
end

if stime >= 0
  set(gca,'XLim', [stime etime]);
end
 
if RTOmin >= 0
  set(gca,'YLim', [RTOmin RTOmax]);
end

grid on;
if oplot | splot | tplot
  strcmd = 'legend(';^M
  if oplot == 1^M
     strcmd = strcat(strcmd, '''RTO'', ');^M
  end^M
  if splot == 1^M
     strcmd = strcat(strcmd,'''smoothed RTT'', ');^M
  end^M
  if tplot == 1^M
     strcmd = strcat(strcmd,'''RTT'', ');^M
  end^M
  strcmd = strcat(strcmd, ' 2);');
  eval(strcmd);^M
else
  display('no data in the chosen time interval');
end

ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),...
           'XAxisLocation','top',...
           'YAxisLocation','right',...
           'Color','none',...
           'xcolor','k','YColor','k');
cplot = 0;
if (~isempty(D)) & (max(D(:,5)) >= 0)
  hold on;
  plot(D(:,1),D(:,5),'-.sg','MarkerSize',2,'Parent',ax2);
  ylabel('RTO counter( ticks )(1 tick = 500 ms)');
  if nargin > 2
%    set(ax2,'XTick',tick);
%    set(ax2,'XTickLabel',' ');
  end
  if stime >= 0
    set(ax2,'XLim', [stime etime]);
  end
  if RTOmin >= 0
    Cmin = RTOmin * 2;
    Cmax = RTOmax * 2;
    set(ax2,'YLim', [Cmin Cmax]);
  end

  cplot = 1;
end


if cplot == 1
   legend('RTO counter', 1);
end
fn1 = strrep(fname1,'_','\_');
fn2 = strrep(fname2,'_','\_');
titcmd = sprintf('title(''%s %s'');',fn1,fn2);
eval(titcmd);

[I,map] = capture;
imcmd = sprintf('imwrite(I,map,''%s.jpg'',''jpeg'');', fnamehd);
eval(imcmd);

set(gcf,'PaperUnits','points');
set(gcf,'PaperPosition',[20 20 800 600]);
print(gcf, '-depsc', fnamehd);
